package com.hualan.sfzy.controller.permission;

import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.Role;
import com.hualan.sfzy.service.impl.permission.RoleServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServiceImpl service;

    @GetMapping
    public Result find() {
        return Result.success(service.find());
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(service.findById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                           Role role) {
        return Result.success(service.findPage(pageNum, pageSize,
                role));
    }

    @PostMapping
    public Result add(@RequestBody Role role) {
        return service.add(role) > 0 ? Result.success() : Result.error();
    }


    // 不允许删除
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id) {

        return Result.error("不允许删除");
    }
//    @DeleteMapping
//    public Result removeBatch(Integer[] ids) {
//
//        return service.removeBatch(ids) > 0 ?Result.success() : Result.error();
//    }

    @PutMapping
    public Result edit(@RequestBody Role role) {
        return service.edit(role) > 0 ? Result.success() : Result.error();
    }
}

